﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace CustomForms
{
    public class SupplementalWebConfigManager
    {
        /// <summary>
        /// 
        /// </summary>
        public static void CreateSupplementalConfig(string connectionString, string destinationFolderPath)
        {
            XDocument document = new XDocument();

            XElement el = new XElement("actions",
               new XElement("add",  new XElement("connectionStrings"), new XAttribute("path", "configuration")),
               new XElement("add", new XElement("add", new XAttribute("name", "FBADB"), new XAttribute("connectionString", connectionString)), new XAttribute("path", "configuration/connectionStrings")),
               new XElement("add", new XElement("membership", new XElement("providers")), new XAttribute("path", "configuration/system.web")),
               new XElement("add", new XElement("add", 
                                        new XAttribute("name", "FBAMembershipProvider"), 
                                        new XAttribute("type", "System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                                        new XAttribute("applicationName", "/"),
                                        new XAttribute("connectionStringName", "FBADB"),
                                        new XAttribute("enablePasswordReset", "true"), 
                                        new XAttribute("enablePasswordRetrieval", "false"),
                                        new XAttribute("passwordFormat", "Clear"),
                                        new XAttribute("requiresQuestionAndAnswer", "false"),
                                        new XAttribute("requiresUniqueEmail", "false")
                                        ), new XAttribute("path", "configuration/system.web/membership/providers")),
               new XElement("add", new XElement("roleManager", new XElement("providers")), new XAttribute("path", "configuration/system.web")),
               new XElement("add", new XElement("add",
                                        new XAttribute("name", "FBARoleProvider"),
                                        new XAttribute("type", "System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                                        new XAttribute("applicationName", "/"),
                                        new XAttribute("connectionStringName","FBADB")
                                        ), new XAttribute("path", "configuration/system.web/roleManager/providers"))        
               
           );

            document.Add(el);

            string result = System.IO.Path.Combine(destinationFolderPath, "webconfig.FBAHelper.xml");
            document.Save(result);
        }

        public static void ModifySTSWebConfig(string connectionString, string destinationFolderPath)
        {
            string path = System.IO.Path.Combine(destinationFolderPath, "web.config");
            XDocument xDoc = XDocument.Load(path);

            XElement lastElement = xDoc.LastNode as XElement;

            if (lastElement != null)
            {

                XElement config = STSConnectionString(connectionString);

                XElement systeWeb = STSMembershipProviders();

                XNode lastNode = (xDoc.LastNode as XElement).LastNode;

                lastNode.AddAfterSelf(systeWeb); 
                lastNode.AddAfterSelf(config);
               
                xDoc.Save(path);
            }
          

        }

        public static void ModifyCentralAdminConfig(string connectionString, string destinationFolderPath)
        {
            string path = System.IO.Path.Combine(destinationFolderPath, "web.config");
            XDocument xDoc = XDocument.Load(path);

            XElement lastElement = xDoc.LastNode as XElement;

            if (lastElement != null)
            {

                XElement config = STSConnectionString(connectionString);


                XNode lastNode = (xDoc.LastNode as XElement).LastNode;

                lastNode.AddAfterSelf(config);

                xDoc.Save(path);
            }
        }

        private static XElement STSConnectionString(string connectionStringValue)
        {
            XElement connString = new XElement("connectionStrings", new XElement("add", new XAttribute("connectionString", connectionStringValue), new XAttribute("name", "FBADB")));


            return connString;
        }

        private static XElement STSMembershipProviders()
        {
            XElement systemWeb = new XElement("system.web");

            XElement membership = new XElement("membership", new XElement("providers",
                                                                new XElement("add", new XAttribute("name", "FBAMembershipProvider"), 
                                                                                    new XAttribute("type", "System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                                                                                    new XAttribute("applicationName", "/"),
                                                                                    new XAttribute("connectionStringName", "FBADB"),
                                                                                    new XAttribute("enablePasswordReset", "true"),
                                                                                    new XAttribute("enablePasswordRetrieval", "false"),
                                                                                    new XAttribute("passwordFormat", "Clear"),
                                                                                    new XAttribute("requiresQuestionAndAnswer", "false"),
                                                                                    new XAttribute("requiresUniqueEmail", "false")
                                                                                    )));


            XElement roleManager = new XElement("roleManager", new XElement("providers",
                                                                new XElement("add", new XAttribute("name", "FBARoleProvider"),
                                                                                    new XAttribute("type", "System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
                                                                                    new XAttribute("applicationName", "/"),
                                                                                    new XAttribute("connectionStringName", "FBADB"))));

            systemWeb.Add(roleManager);

            systemWeb.Add(membership);
                                                                                    
            return systemWeb;
        }
    }

 
}
